﻿using Kanban.Entity;
using Kanban.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kanban.Business
{
    public class LogTarefaBusiness
    {
        private LogTarefaRepository Repository { get; set; }

        public LogTarefaBusiness()
        {
            this.Repository = new LogTarefaRepository();
        }

        public void RegistrarInclusao(Tarefa tarefa, int idUsuario)
        {
            var logTarefa = new LogTarefa();
            logTarefa.IdTarefa = tarefa.Id;
            logTarefa.IdUsuario = idUsuario;
            logTarefa.DataCadastro = DateTime.Now;            
            logTarefa.MnemonicoAcao = LogTarefaAcao.Criar;
            
            this.Repository.Adicionar(logTarefa);
        }


        public void RegistrarAlteracao(Tarefa tarefaAtual, Tarefa tarefaAtualizada, int idUsuario)
        {

            var logTarefa = new LogTarefa();
            logTarefa.IdTarefa = tarefaAtualizada.Id;
            logTarefa.IdUsuario = idUsuario;
            logTarefa.DataCadastro = DateTime.Now;

            if (tarefaAtualizada.Status > 0 && tarefaAtual.Status != tarefaAtualizada.Status)
            {
                this.Repository.RemoverMovimentoAnterior(tarefaAtual.Id);

                logTarefa.MnemonicoAcao = LogTarefaAcao.Mover;
                logTarefa.ValorAntigo = tarefaAtual.Status;
                logTarefa.ValorNovo = tarefaAtualizada.Status;
            }
            else
            {
                logTarefa.MnemonicoAcao = LogTarefaAcao.Editar;
                logTarefa.ValorAntigo = tarefaAtual.Descricao;
                logTarefa.ValorNovo = tarefaAtualizada.Descricao;
            }

            this.Repository.Adicionar(logTarefa);
            
        }
    }
}
